package io.renren.util;

import io.renren.entity.FileBlock;
import io.renren.entity.FileRefBlock;
import io.renren.vo.FileServerResponse;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.entity.ContentType;

import java.util.concurrent.CountDownLatch;


public class DfsThread implements Runnable {

	private CountDownLatch cdl;
	private FileBlock input;
	private FileRefBlock result;

    /**
     * 文件服务器地址
     */
	private String url;

	public DfsThread() {
		super();
	}

	public DfsThread(CountDownLatch cdl, FileBlock input, FileRefBlock result, String url) {
		super();
		this.cdl = cdl;
		this.input = input;
		this.result = result;
		this.url = url;
	}

	@Override
	public void run() {
		result.setName(input.getName());
		result.setStatus("failed");
		byte[] file_content = Base64.decodeBase64(input.getData());

		try {
            FileServerResponse fileServerResponse = DfsUtil.uploadFileByBinary(input.getName(), file_content, ContentType.create(input.getType()), url);
			result.setUrl(fileServerResponse.getHttpUrl());
			result.setStatus("success");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cdl.countDown();
		}
	}
}
